#ifndef DBUSMONITOR_H
#define DBUSMONITOR_H

#include <dbus/dbus.h>
#include <QThread>

class DBusMonitorPrivate;
class DBusMonitor : public QThread {
Q_OBJECT
Q_DECLARE_PRIVATE(DBusMonitor)
public:
    DBusMonitor(DBusBusType type);
    DBusMonitor(const QString &addr);
    bool create_and_init_connection();
    ~DBusMonitor();

    void run() override;
    bool add_rule(const QString &rule);
    bool remove_rule(const QString &rule);
    bool add_rules(const QStringList &rules);
    bool add_handler(DBusHandleMessageFunction handler, void *usr_data=nullptr,
                     DBusFreeFunction fdf=nullptr);
    void remove_handler(DBusHandleMessageFunction handler, void *usr_data=nullptr);
    void stop();
Q_SIGNALS:
    void connectionLost();


public:
    static DBusHandlerResult default_handler(DBusConnection *connection, DBusMessage *message, void *user_data);
    static void print_message(DBusMessage *message);
    static void walk_iter (DBusMessageIter *iter, int depth);
    static void print_iter (DBusMessageIter *iter, int depth);    
    static void print_ay(DBusMessageIter *iter, int depth);
    static void print_hex(const unsigned char*, unsigned, int);
    static void print_fd (int fd, int depth);


private:
    DBusMonitorPrivate * const d_ptr;

};


#endif // DBUSMONITOR_H
